#!/bin/sh

#
# previous versions (<= 17.2.4) had a problem with bvfs after running dbcheck.
# Therefore this test checks
#   * check bvfs root directory
#   * run all dbcheck fixes
#   * check bvfs root directory again
#

. scripts/functions

check_bvfs_root_path()
{
   LOG="${1}"

cat <<END_OF_DATA >${tmp}/bconcmds
@$out ${LOG} w
.bvfs_lsdir jobid=${BackupJobIds} path=
END_OF_DATA

   run_bconsole

   check_log ${LOG}
   # expect:
   # 10    0   0   A A A A A A A A A A A A A A .
   # 9   0   0   A A A A A A A A A A A A A A /
   # 8   0   0   A A A A A A A A A A A A A A @bpipe@/
   if ! grep -q "\W/$" ${LOG}; then
      set_error "root directory not found (using '.bvfs_lsdir path=')."
      exit 1
   fi

   if ! grep -q "\W@bpipe@/$" ${LOG}; then
      set_error "@bpipe@ plugin directory not found (using '.bvfs_lsdir path=')."
      exit 1
   fi
}


# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=susan-fd
JobName=backup-susan-fd
BackupJobIds="1,2"
BvfsPathId="b201"

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"
BackupFileExtra=${BackupDirectory}/extrafile.txt

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# start the test
start_test

# activate bpipe-fd plugin
enable_plugin bpipe-fd

echo "Test Content 1" > ${BackupFileExtra}

# ${tmp}/bconcmds lists the bconsole commands executed by "run_susan"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
@$out ${tmp}/log-backup1.out w
label storage=File1 pool=Default volume=TestVolume001
run job=$JobName fileset=bpipe-fileset storage=File1 yes
wait
messages
quit
END_OF_DATA

# Start the susan daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_susan
check_log ${tmp}/log-backup1.out


# modify file and rerun backup
echo "Test Content 2" >> ${BackupFileExtra}

cat <<END_OF_DATA >${tmp}/bconcmds
@$out ${tmp}/log-backup2.out w
run job=$JobName fileset=bpipe-fileset storage=File1 yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_log ${tmp}/log-backup2.out

${bin}/susan-dbcheck -vvv -b -f > ${tmp}/dbcheck-after-backups.log

cat <<END_OF_DATA >${tmp}/bconcmds
.bvfs_update
@$out ${tmp}/log-bvfs-jobids.out w
.bvfs_get_jobids jobid=2
END_OF_DATA

run_bconsole

LOG=${tmp}/log-bvfs-jobids.out
check_log ${LOG}
# expect: 1,2
if ! grep -q "^[0-9,]*2$" ${LOG}; then
    set_error "failed to find required job ids."
fi

check_bvfs_root_path ${tmp}/log-bvfs-get-root-path-1.out

${bin}/susan-dbcheck -vvv -b -f > ${tmp}/dbcheck-after-bvfs-update.log

check_bvfs_root_path ${tmp}/log-bvfs-get-root-path-2.out

#cat <<END_OF_DATA >${tmp}/bconcmds
#@$out ${tmp}/log-bvfs-get-root-path-offset.out w
#.bvfs_lsdir jobid=${BackupJobIds} path= offset=1000 limit=1000
#
#@$out ${tmp}/log-bvfs-lsdir-root.out w
#.bvfs_lsdir jobid=${BackupJobIds} path=/
#
#@$out ${tmp}/log-bvfs-lsdir-BackupDirectory.out w
#.bvfs_lsdir jobid=${BackupJobIds} path=${BackupDirectory}/
#
#@$out ${tmp}/log-bvfs-lsfiles-BackupDirectory.out w
#@#.bvfs_lsfiles jobid=${BackupJobIds} pathid=1
#.bvfs_lsfiles jobid=${BackupJobIds} path=${BackupDirectory}/
#
#@$out ${tmp}/log-bvfs-lsfiles-BackupDirectory-limit1.out w
#.bvfs_lsfiles jobid=${BackupJobIds} path=${BackupDirectory}/ offset 0 limit 1
#
#@$out ${tmp}/log-bvfs-versions-extrafile.out w
#.bvfs_versions jobid=0 client=${Client} path=${BackupDirectory}/ fname=extrafile.txt
#
#@$out ${tmp}/log-bvfs-lsfiles-bpipe-directory.out w
#.bvfs_lsfiles jobid=${BackupJobIds} path=@bpipe@/
#END_OF_DATA
#
#run_bconsole

cat <<END_OF_DATA >${tmp}/bconcmds
@$out ${tmp}/log-bvfs-cache.out w
.sql query="SELECT count(*) FROM Job WHERE HasCache!=0;"
.sql query="SELECT count(*) FROM PathHierarchy;"
.sql query="SELECT count(*) FROM PathVisibility;"

@$out ${tmp}/log-bvfs-clear-cache.out w
.bvfs_clear_cache yes

@$out ${tmp}/log-bvfs-cleared-cache-hascache.out w
.sql query="SELECT count(*) FROM Job WHERE HasCache!=0;"
@$out ${tmp}/log-bvfs-cleared-cache-PathHierarchy.out w
.sql query="SELECT count(*) FROM PathHierarchy;"
@$out ${tmp}/log-bvfs-cleared-cache-PathVisibility.out w
.sql query="SELECT count(*) FROM PathVisibility;"

quit
END_OF_DATA

run_bconsole

${bin}/susan-dbcheck -vvv -b -f > ${tmp}/dbcheck-after-bvfs-clear-cache.log

cat <<END_OF_DATA >${tmp}/bconcmds
.bvfs_update
END_OF_DATA

run_bconsole

check_bvfs_root_path ${tmp}/log-bvfs-get-root-path-3.out

# verify that all are terminated
check_for_zombie_jobs storage=File1 client=${Client}

# stop susan
stop_susan

LOG=${tmp}/log-bvfs-cleared-cache-hascache.out
check_log ${LOG}
# expect:
# .sql query="SELECT count(*) FROM Job WHERE HasCache!=0;"
# +----------+
# | count(*) |
# +----------+
# | 0        |
# +----------+
#
# Get the 5. line and remove all "|" and " "
COUNT=`sed -e '5!d' -e 's/|//g' -e 's/ //g' $LOG`
if [ "${COUNT}" -ne "0" ]; then
    set_error "after .bvfs_clear_cache HasCache should be 0, not ${COUNT}."
fi

LOG=${tmp}/log-bvfs-cleared-cache-PathHierarchy.out
check_log ${LOG}
COUNT=`sed -e '5!d' -e 's/|//g' -e 's/ //g' $LOG`
if [ "${COUNT}" -ne "0" ]; then
    set_error "after .bvfs_clear_cache PathHierarchy table should be empty (but has ${COUNT} entries)."
fi

LOG=${tmp}/log-bvfs-cleared-cache-PathVisibility.out
check_log ${LOG}
COUNT=`sed -e '5!d' -e 's/|//g' -e 's/ //g' $LOG`
if [ "${COUNT}" -ne "0" ]; then
    set_error "after .bvfs_clear_cache PathVisibility table should be empty (but has ${COUNT} entries)."
fi

# end tests and check for error codes
end_test
